
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.io.*;

// export a .csv file for each senzor in the database

public class CsvSenzorExporter 
{
	public  DBConnect dbConector;
	
	public  String s[];
	public  int rowCount;
	
	public CsvSenzorExporter()
	{
		dbConector = new DBConnect("consum", "root", "");
		rowCount = -1;
	}
	
	public void countFromTable(Connection conn, String component, String table)
	{
		Statement stmt = null;
		ResultSet rs = null;
		try 
		{
			stmt=conn.createStatement();
			//query to count the total number of senzors, put all the values into an array
			rs = stmt.executeQuery("SELECT COUNT(DISTINCT " + component + ") FROM " + table);
			rs.next();
			rowCount = rs.getInt(1);    
		
			s = new String[rowCount + 1];
			rs = stmt.executeQuery("SELECT DISTINCT " + component + " FROM " + table);
			
			rs.next();
			for (int i=0; i<rowCount;i++)
			{
				String value = rs.getString(1);
				//String value = rs.getObject(i).toString();
				s[i] = value;
				rs.next();
			}
		}
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
	}
	
	public void export_csv_file(Connection conn, String name, int type,String path) throws IOException, SQLException
	{
		//export into the csv file all informations about a senzor
		Statement stmt = null;
		try {	
			
			stmt=conn.createStatement();
			
			//if the export is for a sdsl
			if(type == 0)
				//query to export .csv tables for each Subject
				//stmt.executeQuery("SELECT Actiune, Data, Start, Stop FROM sdsl WHERE Subiect = '"+ name +"' INTO OUTFILE '"+name+".csv' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'");
				stmt.executeQuery("SELECT CONCAT(CONCAT(Data,CONCAT('T',Start)),'Z') ,Actiune FROM sdsl WHERE Subiect = '"+ name +"' INTO OUTFILE '"+path+"' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'");
			else
				//query to export .csv tables for each senzor
				stmt.executeQuery("SELECT CONCAT(CONCAT(CURDATE(),CONCAT('T',ora)),'Z') ,valoare FROM scenariu WHERE senzor = '"+ name +"' INTO OUTFILE '"+name+".csv' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'");
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
	}
	
	public void extractCSV(Sdsl2BdGui gui, Connection conn, String component, String table, int type)
	{
		Patterns pat = new Patterns();
		countFromTable(conn, component, table);
		
		gui.reportText.append(
				"Id    Activities\n" + 
				"0    = se trezeste\n"+
				"1    = se culca\n"+
				"2    = merge\n"+
				"3    = pleaca\n"+
				"4    = vine\n"+
				"5    = mananca\n"+
				"6    = bea\n"+
				"7    = gateste\n"+
				"8    = merge la WC\n"+
				"9    = spala\n"+
				"10  = porneste\n"+
				"11  = opreste\n"+
				"12  = foloseste televizor\n"+
				"13  = foloseste calculator\n"+
				"14  = foloseste masina de spalat\n"+
				"15  = foloseste aragaz\n"+
				"16  = seteaza\n");
		
		for(int i = 0; i < rowCount; i++)
		{
			try 
			{
				File f1 = new File("activities.csv");
					f1.delete();
				
				export_csv_file(conn, s[i], type,"E:\\WORKSPACE/TIMESERIES/TSF/activities.csv");
				pat.main("activities.csv", 3);
				
				f1 = new File("E:\\WORKSPACE/TIMESERIES/TSF/activities.csv");
				f1.delete();
				
				File inputFile1 = new File("patterns.txt");
			    FileReader fr1 = new FileReader(inputFile1);
			    BufferedReader br1 = new BufferedReader(fr1);
			    
			    File inputFile2 = new File("frequencies.txt");
			    FileReader fr2 = new FileReader(inputFile2);
			    BufferedReader br2 = new BufferedReader(fr2);

			    gui.reportText.append("Activity habbits for " + s[i] + " : \n");
			    String patternLine = new String();
			    String frequencyLine = new String();
			    String[] tokens = new String[2];
			    
			    while (((patternLine=br1.readLine())!=null) && ((frequencyLine=br2.readLine())!=null)) 
			    {
			    	tokens = frequencyLine.split("\t");
			    	gui.reportText.append(patternLine + "\t"+ tokens[1] + "\n");
			    	
			    	patternLine = br1.readLine();
			    	gui.reportText.append(patternLine + "\n");
			    	patternLine=br1.readLine();
			    	gui.reportText.append(patternLine + "\n");
			    	
			    	frequencyLine=br2.readLine();
			    	gui.reportText.append(frequencyLine + "\n");
			    	frequencyLine=br2.readLine();
			    	gui.reportText.append(frequencyLine + "\n");
			    	
			    	//for the blank lines
			    	patternLine = br1.readLine();
			    	frequencyLine=br2.readLine();
			    	gui.reportText.append("\n");
			    }
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		System.out.println("The files were exported in the DataBase's folder");
	}
}
